//
//  HZL_AllViewController.m
//  B_Project
//
//  Created by 胡佳俊 on 15/11/29.
//  Copyright © 2015年 HZL. All rights reserved.
//

#import "HZL_AllViewController.h"
#import "HZL_AllCollectionViewCell.h"
#import "HZL_AllModel.h"
#import "HZL_SceneViewController.h"
@interface HZL_AllViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic, retain) UICollectionView *collectionView;
@property (nonatomic, retain) NSMutableArray *headerArr;
@property (nonatomic, retain) NSMutableArray *foodArr;
@property (nonatomic, retain) NSMutableArray *relaxationArr;
@end

@implementation HZL_AllViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"全部类型";
    self.headerArr = [NSMutableArray array];
    self.foodArr = [NSMutableArray array];
    self.relaxationArr = [NSMutableArray array];
    [self getData];
    [self createCollectionView];
    
    [self loadMBProgress];
}
#pragma mark - 准备数据
- (void)getData
{
    NSString *body = @"cityId=xiamen";
    [HZL_NetworkingHandle requestPOSTWithURl:@"http://www.molyo.com/mShop/category/getAllChannelCategoryV1_7" body:body completion:^(id result) {
        self.hud.hidden = YES;
        self.headerArr = [NSMutableArray arrayWithArray:result[@"body"][@"channel"]];
        self.relaxationArr = [HZL_AllModel transformWithArray:self.headerArr[0][@"categorys"]];
        self.foodArr = [HZL_AllModel transformWithArray:self.headerArr[1][@"categorys"]];
        [self.hud removeFromSuperview];
        [self.collectionView reloadData];
    }];
}
#pragma mark - 创建CollectionView
- (void)createCollectionView
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.itemSize = CGSizeMake((VIEW_WIDTH - 70) / 4, 60);
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    layout.sectionInset = UIEdgeInsetsMake(10, 20, 10, 20);
    layout.headerReferenceSize = CGSizeMake(100, 30);
    layout.minimumInteritemSpacing = 10;
    // 行间距
    layout.minimumLineSpacing = 10;
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, VIEW_WIDTH, VIEW_HEIGHT) collectionViewLayout:layout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.collectionView];
    
    [self.collectionView registerClass:[HZL_AllCollectionViewCell class] forCellWithReuseIdentifier:@"item"];
    
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
}
#pragma mark - item点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    HZL_SceneViewController *sVC = [[HZL_SceneViewController alloc] init];
    if (indexPath.section == 0) {
        sVC.allId = [self.relaxationArr[indexPath.item] allId];
    }else {
        sVC.allId = [self.foodArr[indexPath.item] allId];
    }
    NSLog(@"%@", sVC.allId);
    [self.navigationController pushViewController:sVC animated:YES];
    
}
#pragma mark - 头部个数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return self.headerArr.count;
}
#pragma mark - 必须实现的两个方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (section == 0) {
        return self.relaxationArr.count;
    }else {
        return self.foodArr.count;
    }
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    HZL_AllCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"item" forIndexPath:indexPath];
    if (indexPath.section == 0) {
        cell.model = self.relaxationArr[indexPath.item];
    }else {
        cell.model = self.foodArr[indexPath.item];
    }
    cell.backgroundColor = [UIColor colorWithRed:0.6 green:0.6 blue:0.6 alpha:0.5];
    return cell;
}
#pragma mark - t头部/尾部区域内容
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    // 判断kind类型 设置头部/尾部
    if (kind == UICollectionElementKindSectionHeader) {
        // 从重用池中获取头部
        UICollectionReusableView *headView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
//        headView.backgroundColor = [UIColor redColor];
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 25)];
        if (indexPath.section == 0) {
            label.text = self.headerArr[0][@"name"];
        }
        else {
            label.text = self.headerArr[1][@"name"];
        }
        UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(10, 28, VIEW_WIDTH - 20, 1)];
        line.backgroundColor = [UIColor blackColor];
        line.alpha = 0.5;
        [headView addSubview:line];
        [headView addSubview:label];
        return headView;
    }
    return nil;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
